package com.yixun.qfbao.hander;

import com.alibaba.fastjson.JSONObject;
import com.yixun.qfbao.api.service.CPReportService;
import com.yixun.qfbao.api.service.CompanyService;
import com.yixun.qfbao.api.service.ReceiveIntentionService;
import com.yixun.qfbao.apidto.ReceiveIntentionReqDto;
import com.yixun.qfbao.apidto.ReceiveIntentionResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.util.WxAccessTokenCacheUtils;
import com.yixun.qfbao.utils.TokenUtils;
import com.yixun.qfbao.vo.ReceiveIntentionReqVo;
import com.yixun.qfbao.xf.model.Premises;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.PremisesService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("receiveIntentionHander")
public class ReceiveIntentionHander extends CommonAbstractHander {

    private Logger logger = LoggerFactory.getLogger(ReceiveIntentionHander.class);

    @Autowired
    CPReportService cpReportService;
    @Autowired
    TokenUtils tokenUtils;
    @Autowired
    private WxAccessTokenCacheUtils wxAccessTokenCacheUtils;
    @Autowired
    private PremisesService premisesService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    ReceiveIntentionService receiveIntentionService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<ReceiveIntentionResDto> res = new BaseResponseDTO<ReceiveIntentionResDto>();
        ReceiveIntentionResDto resDto = new ReceiveIntentionResDto();
        ReceiveIntentionReqVo reqVo = (ReceiveIntentionReqVo)reqDto;
        ReceiveIntentionReqDto receiveIntentionReqDto = reqVo.getParams();
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        try{
            // 1.验证参数
            if(receiveIntentionReqDto.getEmployeeUuid() == null){
                res.setResult(ResultEnum.FAIL.getNo());
                res.setErrorMsg("v10报备人id为空");
                res.setResData(resDto);
            }
            if(receiveIntentionReqDto.getEmployeePhone() == null){
                res.setResult(ResultEnum.FAIL.getNo());
                res.setErrorMsg("v10用户手机为空");
                res.setResData(resDto);
            }
            if(receiveIntentionReqDto.getPremisesId() == null){
                res.setResult(ResultEnum.FAIL.getNo());
                res.setErrorMsg("v10报备人id为空");
                res.setResData(resDto);
            }
            if(receiveIntentionReqDto.getReportVisit() == null){
                res.setResult(ResultEnum.FAIL.getNo());
                res.setErrorMsg("约看信息不能为空");
                res.setResData(resDto);
            }

            String companyUuid = null;
            String agencyToken = null;
            String assistantToken = null;
            Premises premises = premisesService.selectSimpleById(Long.valueOf(receiveIntentionReqDto.getPremisesId()));
            logger.info("V10报备,获取消息推送信息 premises info:{}", JSONObject.toJSONString(premises));
            if(null != premises){
                Company company = companyService.getCompanyById(premises.getCompanyId());
                logger.info("V10报备,获取消息推送信息 company info:{}", JSONObject.toJSONString(company));
                if(null != company){
                    companyUuid = company.getCompanyUuid();
                }
            }
            if(StringUtils.isNotEmpty(companyUuid)){
                logger.info("V10报备,获取消息推送信息 companyUuid:{}", companyUuid);
                agencyToken = wxAccessTokenCacheUtils.getAccessToken(companyUuid, SystemIdEnum.AGENCY_h5.getCode(), null);
                logger.info("V10报备,获取消息推送信息 agencyToken:{}", companyUuid);
                assistantToken = wxAccessTokenCacheUtils.getAccessToken(companyUuid, SystemIdEnum.ASSISTANT.getCode(), null);
            }

            logger.info("V10报备,获取消息推送信息 assistantToken:{}", assistantToken);
            receiveIntentionService.receiveIntention(receiveIntentionReqDto,agencyToken,assistantToken,companyUuid);
        }catch (Exception e){
            logger.error("ReceiveIntentionHander error: {}", e);
            resDto.setResultMsg(e.getMessage());
        }
        res.setResult(resDto.getResult());
        res.setResData(resDto);
        return res;
    }

}
